<template>
  <div class="tabbar">
    <ul>
      <li
        v-for="(item, index) in routerList"
        :key="index"
        @click="switchTab(item.path)"
      >
        <img
          :src="$route.path.includes(item.path) ? item.selected : item.active"
          alt=""
        />
        <span :class="$route.path.includes(item.path) ? 'active' : ''">{{
          item.title
        }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "Tabbar",
  data() {
    return {
      routerList: [
        {
          title: "首页",
          path: "/home",
          active: "./tab_icons/home.png",
          selected: "./tab_icons/home-active.png",
        },
        {
          title: "分类",
          path: "/list",
          active: "./tab_icons/cate.png",
          selected: "./tab_icons/cate-active.png",
        },
        {
          title: "购物车",
          path: "/cart",
          active: "./tab_icons/cart.png",
          selected: "./tab_icons/cart-active.png",
        },
        {
          title: "我的",
          path: "/my",
          active: "./tab_icons/my.png",
          selected: "./tab_icons/my-active.png",
        },
      ],
    };
  },
  methods: {
    switchTab(path) {
      //判断是否是同一个路由
      if (this.$route.path == path) return;

      this.$router.replace(path);
    },
  },
};
</script>

<style lang="less" scoped>
.tabbar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1.6rem;
  background-color: #fff;
  z-index: 999;
  align-items: center;
  border-top: 0.0267rem solid #eee;
  ul {
    display: flex;
    justify-content: space-around;
    width: 100%;
    height: 100%;
    li {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      img {
        width: 0.8267rem;
        height: 0.8267rem;
      }
      span {
        font-size: 0.4267rem;
      }
    }
  }
}
.active {
  color: red;
}
</style>